package com.mdoya.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mdoya.domain.ShoppingCart;
import com.mdoya.exception.BusinessExceptiion;
import com.mdoya.mapper.ShoppingCartMapper;
import com.mdoya.service.ShoppingCartService;
import com.mdoya.utils.ThreadLocalContextUtils;
import com.sun.prism.impl.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author mdoya
 * @version 1.0
 * @description:
 */
@Slf4j
@Scope("singleton")
@Transactional(propagation = Propagation.REQUIRED)
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    public List<ShoppingCart> list() {
        return shoppingCartMapper.selectList(null);
    }

    @Override
    public Integer add(ShoppingCart shoppingCart) {
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCart.setUserId(ThreadLocalContextUtils.getUserLongId());
        return shoppingCartMapper.insert(shoppingCart);
    }

    /**
     * 根据用户id查找在购物车物品
     *
     * @param userLongId
     * @return
     */
    @Override
    public List<ShoppingCart> findUserShoppinCar(Long userLongId) {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userLongId);
        return shoppingCartMapper.selectList(queryWrapper);
    }

    /**
     * 清空购物车
     *
     * @param userLongId
     * @return
     */
    @Override
    public Integer cleanShoppingCar(Long userLongId) {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userLongId);
        return shoppingCartMapper.delete(queryWrapper);
    }

    /**
     * 购物车数量处理
     *
     * @param map
     * @return
     */
    @Override
    public Integer ShoppingCarCount(Map<String, String> map, Long userId) {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        // 当前操作用户
        System.err.println("用户id  = " + userId);
        queryWrapper.eq(ShoppingCart::getUserId, userId);
        String dishId = map.get("dishId");
        String setmealId = map.get("setmealId");
        System.err.println("dishId：" + dishId);
        System.err.println("setmealId：" + setmealId);
        int rows = 0;
        ShoppingCart shoppingCart = null;
        // 套餐
        if (StringUtils.isNotEmpty(setmealId)) {
            queryWrapper.eq(setmealId != null , ShoppingCart::getSetmealId, setmealId);
            shoppingCart = shoppingCartMapper.selectOne(queryWrapper);
            if (shoppingCart.getNumber() > 0) {
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                rows = shoppingCartMapper.updateById(shoppingCart);
            }

        } else { // 菜品
            queryWrapper.eq(dishId != null, ShoppingCart::getDishId, dishId);
            shoppingCart = shoppingCartMapper.selectOne(queryWrapper);
            System.err.println("对象：  " + shoppingCart);
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            rows = shoppingCartMapper.updateById(shoppingCart);
            // 菜品为0 清空该菜品
            if (shoppingCart.getNumber() == 0) {
                shoppingCartMapper.deleteById(shoppingCart.getId());
            }
        }
        return rows;
    }
}
